<?php 
namespace Content\Controller;
use Think\Upload;
class ImageController extends CommonController{
	
	/**
	 * 执行默认程序
	 */
	function _initialize() {
		//$this->notNeedLoginArr = array('index');
		parent::_initialize();
	}
	
	
	/**
	 * @ajax上传图片
	 */
	public function upload(){
		
	 	$config = C('UPLOAD');
        $upload = new \Think\Upload($config);// 实例化上传类
        $images = $upload->upload();
        if ($images) {
        	$images['url'] = $config['rootPathBase'].$images['file']['savepath'].$images['file']['savename'];
            _apiReturn(1, 'success', $images, $url);
        } else {
        }
	}

	/**
	* 获取图片列表数据
	*/
	public function getdata(){

		$p = I('p') ? I('p') : 1;
		$row = I('row') ? I('row') : 10;
	
		$m = D('MaterialImages');
		if(I('condition')) {
            $condition = condition_to_array(I('condition')); //加入条件参数
            foreach($condition as $key=>$val){
                $map[$key] = $val;
            }
        }
		$data['data'] = $m->where($map)->page($p,$row)->select();
		//$data['sql'] = $m->_sql();
		$data['page']['total'] = $m->where($map)->count();
		$data['page']['cur'] = $p;
		$data['page']['row'] = $row;
	
		_apiReturn(1, L('success'), $data, $url); //数据获取成功
	}
	
	/**
	* 七牛图片上传
	*/
	public function qiniu_upload(){
		
		if(!empty($_FILES)){
			$tmpfile = $_FILES['file'];
			if(!$tmpfile){
				$tmpfile = $_FILES['upfile'];
			}
			$max_size = 10000000000;
			if(!$_REQUEST['type']){
				_apiReturn(0,"请配置上传文件类型，可选参数：image/flash/media/file");
			}

			$config = C('QINIU_CONFIG');
		
			$qiniu = new \Think\Upload\Driver\Qiniu($config);	
			$m = D('MaterialImages');

			$extArr = array(
					'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
					'flash' => array('swf', 'flv'),
					'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
					'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
			);
			//原文件名
			$file_name = $tmpfile['name'];
			//服务器上临时文件名
			$tmp_name = $tmpfile['tmp_name'];
			//文件大小
			$file_size = $tmpfile['size'];
			//检查文件名
			if (!$file_name) {
				_apiReturn(0,"请选择文件。");
			}
			//检查文件大小
			if ($file_size > $max_size) {
				_apiReturn(0,"上传文件大小超过限制。");
			}
				
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			
			if (in_array($file_ext, $extArr[$_REQUEST['type']]) === false) {
				_apiReturn(0,"上传文件扩展名是不允许的扩展名。\n只允许' . implode(",", $this->extArr['image']) . '格式。");
			}

			if($_REQUEST['type'] == 'image'){
				if($file_size > 3072000){
					//图片大小不能大于3M
					if($_REQUEST['dofor'] == 'ueditor'){
						echo "图片不能大于3M";
						die();
					}else{
						_apiReturn(0,'图片大小不能超过3M');
					}
				}
			}
			
				
			$str = new \Org\Util\String();
				
			$file['savename'] = $str->keyGen().'.'.$file_ext;
			$file['savepath'] = '';
			$file['tmp_name'] = $tmp_name;
				
				
			//上传到七牛服务器
			if($qiniu->save($file)){
				//存储数据组装
				$data['key'] = $file['savename'];
				$data['url'] = $qiniu->downLink($file['savename']);
				$data['create_time'] = date('Y-m-d H:i:s');
				$data['size'] = $file_size;
				$data['uid'] = $_SESSION['loginUser']['uid']; //存储上传者ID
				$data['title'] = $file_name;
					
				
				
				$imgID = $m->add($data);

	
				if($imgID){

					if($_REQUEST['dofor'] == 'ueditor'){

						//header('Content-Type:application/json; charset=utf-8');
						//header("Access-Control-Allow-Origin: *");  //跨域请求

						$resultData = array(
							"originalName" =>$file_name ,
							"name" => $data['key'] ,
							"url" => $data['url'] ,
							"size" => $data['size'] ,
							"type" =>  '.'.$file_ext,
							"state" => "SUCCESS"
						);

						echo json_encode($resultData);
						die();
					}else{
						$resultData = array(
							"url"=>$data['url'],
							"time"=>$data['create_time'],
							"id"=>$imgID,
							"listorder" => 0,
							"other"=>$_POST,
							"title"=>$file_name,
							"redurl" => ""
						);
						_apiReturn(1,"success",$resultData);
					}
					
						
					
				}else{
					_apiReturn(0,"存储数据组装异常。");
				}
			}else{
				_apiReturn(0,"服务器上传失败，请检查您的七牛配置。");
			}
				
		}else{
			_apiReturn(0,'请选择文件');
		}
	}

	public function del(){
		
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}

		$config = C('QINIU_CONFIG');
		$qiniu = new \Think\Upload\Driver\Qiniu($config);
		$m = D('MaterialImages');
		$map = array($m->getPk() => array('in', $id));
		$lists = $m->where($map)->select();
		if($m->where($map)->delete()){ //删除本地数据库中的row
			foreach($lists as $vo){
				$qiniu->del($vo['key']); //删除七牛远程图片
			}
			$this->success('删除成功');
		} else {
			$this->error('删除失败！');
		}
	}
	
}
?>